<?php
/**
 * 自定义CMD命令 - HTTP 请求监控组件创建
*/

namespace houze\cmd;
use houze\Console;
use houze\SystemTips;

class Monitor {
    /**
     * 命令行参数
    */
    private $argv=[];

    /**
     * 调用入口
    */
    public function run($argv) {
        $this->argv = $argv;
        if (empty($argv[2])) return Console::exit_error(SystemTips::HTTP_MONITOR_1 . PHP_EOL);
        if ($argv[2] != 'start') return Console::exit_error(SystemTips::HTTP_MONITOR_2 . PHP_EOL);

        $this->copy_controller();
        $this->copy_view();

        $html  = 'HTTP请求监控Web组件安装完成！'.PHP_EOL.PHP_EOL;
        $html .= 'HTTP监控台-WEB路由地址：/HttpMonitor/login'.PHP_EOL;
        $html .= '初始化账号密码在：/config/server.php 文件中进行修改。'.PHP_EOL;

        return Console::exit_error($html, false);
    }

    /**
    */
    private function copy_controller() {
        $dir_create = APP_PATH.'http/controller/console/';
        $dir = APP_PATH.'http/controller/console/HttpMonitor.php';
        
        if (!file_exists($dir_create)) mkdir($dir_create, 0755);
        if (file_exists($dir)) return false;

        return copy(BUILT_PATH.'monitor/controller/HttpMonitor.php', $dir);
    }

    /**
     * 复制视图到指定位置
    */
    private function copy_view() {
        $dir = APP_PATH.'http'.DS.'view'.DS.'HttpMonitor'.DS;
        if (file_exists($dir)) return false;
        mkdir($dir, 0755);

        $file = BUILT_PATH.'monitor/view/';
        $temp = scandir($file);
        //遍历文件夹
        foreach ($temp as $v){
            if ($v=='.' || $v=='..') continue;
            copy($file.$v, $dir.$v);
        }

        return true;
    }

}